import { SDKDataMod } from "../SDKDataMode";
import { TaskData } from "../任务管理类/TaskData";
import { ADMgr } from "../广告播放类/ADMgr";
import { AdData } from "./AdData";
import { OpenIDData } from "./OpenIDData";

export class WebDataProxy {
    private static _inst: WebDataProxy = null;
    public static get inst(): WebDataProxy {
        if (!this._inst) {
            this._inst = new WebDataProxy();
        }
        return this._inst;
    }
    private constructor() { }

    public AdWebData: AdData = new AdData();

    public TaskList: TaskData[] = []; // 任务列表

    public SetAdWebData(data: SDKDataMod.AdWebData) {
        if(data == null) return;
        this.AdWebData.SetData(data);
        for (let i = 0; i < data.reward_task.length; i++) {
            const element = data.reward_task[i];
            let task = new TaskData();
            task.SetData(element);
            task.Init();
            this.TaskList.push(task);
        }
        ADMgr.初始化广告位(data.reward_adunits.map((v) => v.ad_unit));
    }

    public get m_TaskFinishCount(): number {
        let count = 0;
        for (let i = 0; i < this.TaskList.length; i++) {
            const element = this.TaskList[i];
            count += element.m_TaskFinishCount;
        }
        return count;
    }

    public get m_TaskFinish(): boolean {
        let count = 0;
        for (let i = 0; i < this.TaskList.length; i++) {
            const element = this.TaskList[i];
            if (element.m_CheckTaskState == "完成") {
                count++;
            }
        }
        return count == this.TaskList.length;
    }

    public GetTaskData(index: number): TaskData {
        for (let i = 0; i < this.TaskList.length; i++) {
            const element = this.TaskList[i];
            if (i == index) {
                return element;
            }
        }
        return null;
    }

    public OpenIDData: OpenIDData = new OpenIDData(); 
}